<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="referrer" content="no-referrer"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 1200px;
            margin: 0 auto;
        }
        ul,li{
            list-style: none;
        }
        ul{
            width: 260px;
            margin-left: 20px;
            float: left;
        }
        ul li{
            border: 1px solid gray;
        }
        ul li .imgBox{
            width: 260px;
        }
        img{
            width: 100%;
            height: 100%;
            display: block;
        }
        ul li .description{
            padding: 20px;
            font-size: 12px;
        }
        ul li .description .wenyuan{
            display: flex;
            align-items: center;
        }
        ul li .description .wenyuan .left{
            width: 25px;
            height: 25px;
            border-radius: 50%;
            overflow: hidden;
            margin-right: 10px;
        }
    </style>
</head>
<body>
    <div class="box">
        <ul></ul>
        <ul></ul>
        <ul></ul>
        <ul></ul>
    </div>



    <script>
        let flag = true;
        // https://www.duitang.com/napi/blog/list/by_filter_id/?
        // include_fields=top_comments%2Cis_root%2Csource_link%2Citem%2Cbuyable%2Croot_id%2Cstatus%2Clike_count%2Csender%2Calbum%2Creply_count&filter_id=%E6%89%8B%E5%B7%A5DIY&start=24&_=1655363994353
        let start = 0;
        getData();
        function getData(){
            const xhr = new XMLHttpRequest()
            xhr.open("GET",`/dt?include_fields=top_comments%2Cis_root%2Csource_link%2Citem%2Cbuyable%2Croot_id%2Cstatus%2Clike_count%2Csender%2Calbum%2Creply_count&filter_id=%E6%89%8B%E5%B7%A5DIY&start=${start}&_=1655364636042`);
            xhr.send();
            xhr.onload = function(){
                console.log(JSON.parse(xhr.responseText));
                render(JSON.parse(xhr.responseText))
            }
        }

        /* 利用数据渲染视图 */
        function render(data){
            // console.log(data.data.object_list);
            let arr = data.data.object_list
            //遍历arr数组
            arr.forEach(item => {
                // console.log(item);
                let str = `
                <li>
                    <div class="imgBox">
                        <img src="${item.photo.path}" alt="">
                    </div>
                    <div class="description">
                        <p>${item.msg}</p>
                        <div class="wenyuan">
                            <div class="left">
                                <img src="${item.sender.avatar}" alt="">
                            </div>
                            <div class="right">
                                <div class="top">${item.sender.username}</div>
                                <div class="bottom">发布到  ${item.album.name}</div>
                            </div>
                        </div>
                    </div>
                </li>
                `
                // 哪一个UL最矮,就将str到哪一个ul的最后面
                let uls = document.querySelectorAll("ul")
                //默认第一个是最矮的
                let minUl = uls[0]
                let minUlHeight = uls[0].offsetHeight;

                for(let i = 0 ; i < uls.length ; i++){
                    /* 比较,获取最矮的那个Ul */
                    if(minUlHeight > uls[i].offsetHeight){
                        minUlHeight = uls[i].offsetHeight
                        minUl = uls[i]
                    }
                }
                //将我str扔到最矮的ul下面
                minUl.innerHTML += str;

                /* 加载完毕,打开开关 */
                flag = true;
            });
        }


        window.onscroll = function(){
            /* 开关思想 */
            if(!flag)return;
            flag = false;


            //获取视口的高度
            let windowHeight = document.documentElement.clientHeight
            //获取卷出的高度
            let scrollTop = document.documentElement.scrollTop || document.body.scrollTop
            //计算最矮的ul的高度
            let uls = document.querySelectorAll("ul")
            //默认第一个是最矮的
            let minUl = uls[0]
            let minUlHeight = uls[0].offsetHeight;
            for(let i = 0 ; i < uls.length ; i++){
                /* 比较,获取最矮的那个Ul */
                if(minUlHeight > uls[i].offsetHeight){
                    minUlHeight = uls[i].offsetHeight
                    minUl = uls[i]
                }
            }
            // console.log(windowHeight,scrollTop,minUlHeight);
            //触底
            // 重新获取数据
            setTimeout(() => {
                start += 24;
                getData();
            }, 1000);
        }
    </script>
</body>
</html>